package com.infinitus.nginxInfo.task;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import com.infinitus.nginxInfo.domain.nginxinfo.NginxInfo;
import com.infinitus.nginxInfo.domain.switchinfo.SwitchInfo;
import com.infinitus.nginxInfo.service.util.TimeUtil;


public class FilterRules {

	public static Date expiryDate ;
	static{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DATE, -7);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		expiryDate = calendar.getTime();
	}
	
	public static boolean isNotBlank(String str){
		if(str != null && !"".equals(str.trim())){
			return true;
		}
		return false;
	}
	
	/**
	 * 时间过滤
	 * Event_DateTime >= 现在 – 7天 AND Event_DateTime <= 现在
	 * @param time
	 * @return
	 * @throws ParseException 
	 */
	public static boolean checkTime(String time){
		if(time==null){
			return false;
		}
		try {
			Date eventTime = new Date(Long.parseLong(time));
			
			Date now = new Date();
			if(eventTime.after(now)){
				return false;
			}
			if(expiryDate==null){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, -7);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				expiryDate = calendar.getTime();
			}
			if(eventTime.before(expiryDate)){
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 时间过滤
	 * Event_DateTime >= 现在 – 7天 AND Event_DateTime <= 现在
	 * @param timeStr
	 * @return
	 * @throws ParseException 
	 */
	public static boolean checkTimeStr(String timeStr){
		try {
			Date eventTime = TimeUtil.dateTimeFormat.parse(timeStr);
			
			Date now = new Date();
			if(eventTime.after(now)){
				return false;
			}
			if(expiryDate==null){
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DATE, -7);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				expiryDate = calendar.getTime();
			}
			if(eventTime.before(expiryDate)){
				return false;
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static String checkAll(NginxInfo one){
		if(!checkTime(one.getEvent_time())) return "eventDateTime_error";
		return null;
	}
	
	public static String checkAll(SwitchInfo one){
		if(!checkTime(one.getEvent_time())) return "eventDateTime_error";
		return null;
	}
	
	public static void main(String[] args){
	}
}
